// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/buffer_util.h>
#include <pollux/dwio/common/selective_column_reader_internal.h>
#include <pollux/dwio/dwrf/common/decoder_util.h>
#include <pollux/dwio/dwrf/reader/dwrf_data.h>

namespace kumo::pollux::dwrf {

using namespace dwio::common;

template <typename DataT>
class SelectiveDecimalColumnReader : public SelectiveColumnReader {
 public:
  SelectiveDecimalColumnReader(
      const std::shared_ptr<const TypeWithId>& fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  bool hasBulkPath() const override {
    // Only ORC uses RLEv2 encoding. Currently, ORC decimal data does not
    // support fastpath reads. When reading RLEv2-encoded decimal data
    // with null, the query will fail.
    return version_ != pollux::dwrf::RleVersion_2;
  }

  void seekToRowGroup(int64_t index) override;
  uint64_t skip(uint64_t numValues) override;

  void read(int64_t offset, const RowSet& rows, const uint64_t* nulls) override;

  void getValues(const RowSet& rows, VectorPtr* result) override;

 private:
  template <bool kDense>
  void readHelper(RowSet rows);

  std::unique_ptr<IntDecoder<true>> valueDecoder_;
  std::unique_ptr<IntDecoder<true>> scaleDecoder_;

  BufferPtr scaleBuffer_;
  RleVersion version_;
  int32_t scale_ = 0;
};

} // namespace kumo::pollux::dwrf
